package cn.lyd.controller;

import cn.lyd.domain.Student;
import cn.lyd.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/system/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    /**
     * 查询学员列表
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String findAll(Model model){
        List<Student> list = studentService.findAll();
        model.addAttribute("list",list);
        return "student_list";
    }

    /**
     * 删除学员
     * @param id
     * @return
     */
    //删除 /del
    @RequestMapping("/del")
    public String delById(Long id){
        studentService.delete(id);
        //重定向，重新查询所有学员的接口
        return "redirect:list";//相对路径，直接在当前请求下找
//        return "redirect:/system/student/list";//绝对路径
    }


    /**
     * 新增或修改跳转
     * @param id
     * @param model
     * @return
     */
    //新增跳转
    @RequestMapping("/jump")
    public String jump(Long id,Model model){
        if (id==null) {//执行新增
            //新增跳转
            return "student_add";
        } else {
            //根据id查询学员信息
            Student student = studentService.loadById(id);
            model.addAttribute("student",student);
            //执行修改跳转
            return "student_edit";
        }
    }


    /**
     * 新增学员
     * @param student
     * @return
     */
    //新增 /save
    @RequestMapping("/save")
    public String save(Student student){
        studentService.save(student);
        //重定向，重新查询所有学员的接口
        return "redirect:list";//相对路径，直接在当前请求下找
//        return "redirect:/system/student/list";//绝对路径
    }


    /**
     * 修改学员
     * @param student
     * @return
     */
    //修改 /update
    @RequestMapping("/update")
    public String update(Student student){
        studentService.update(student);
        //重定向，重新查询所有学员的接口
//        return "redirect:list";//相对路径，直接在当前请求下找
        return "redirect:/system/student/list";//绝对路径
    }


}
